cmake_minimum_required(VERSION 3.14)
project(kalman_catkin)

find_package(catkin REQUIRED)

include(FetchContent)

# Define directories
set(KALMAN_DEVEL_PREFIX ${CATKIN_DEVEL_PREFIX} CACHE STRING "KALMAN install path")
set(KALMAN_INCLUDE_DIR ${KALMAN_DEVEL_PREFIX}/include)
set(KALMAN_LIB_DIR ${KALMAN_DEVEL_PREFIX}/lib)
set(KALMAN_DOWNLOAD_DIR ${CMAKE_CURRENT_BINARY_DIR}/download)
set(KALMAN_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR}/build)

# Create directories if they do not exist
file(MAKE_DIRECTORY ${KALMAN_INCLUDE_DIR})
file(MAKE_DIRECTORY ${KALMAN_LIB_DIR})
file(MAKE_DIRECTORY ${KALMAN_DOWNLOAD_DIR})
file(MAKE_DIRECTORY ${KALMAN_BUILD_DIR})

# Kalman Settings
set(KALMAN_BUILD_EXAMPLES OFF CACHE BOOL "Examples disable")

# Download & build source
FetchContent_Declare(kalmanDownload
        GIT_REPOSITORY https://gitee.com/craipy/kalman.git
        GIT_TAG d47028d2efa1ee281dd44dd01dac81c25661004f
        UPDATE_COMMAND ""
        SOURCE_DIR ${KALMAN_DOWNLOAD_DIR}
        BINARY_DIR ${KALMAN_BUILD_DIR}
        BUILD_COMMAND $(MAKE)
        INSTALL_COMMAND "$(MAKE) install"
        )
FetchContent_MakeAvailable(kalmanDownload)

# Copy header to where catkin expects them

file(GLOB_RECURSE HEADERS "${KALMAN_DOWNLOAD_DIR}/include/kalman/*")
foreach (HEADER_FILE ${HEADERS})
    message(STATUS "FOUND HEADER: " ${HEADER_FILE})
    file(COPY ${HEADER_FILE} DESTINATION ${KALMAN_INCLUDE_DIR}/kalman)
endforeach ()

catkin_package(
        INCLUDE_DIRS ${KALMAN_INCLUDE_DIR}
)
